#include "set_uart.h"



volatile char rx_buffer0[RX_BUFFER_SIZE0];
volatile unsigned char rx_wr_index0=0;
volatile unsigned char rx_rd_index0=0;
volatile unsigned char rx_counter0=0;
volatile char rx_buffer_overflow0=0;

void myInit_UART(void)
{
	//Mode Asynchronous, disable parity, 1 stop bit, 8 bit char
	USARTC0.CTRLC= USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | (0<<USART_SBMODE_bp) | USART_CHSIZE_8BIT_gc;
	
	//Set BaudRate 115200, BSCALE = -7 , BSEL=2094
	USARTC0.BAUDCTRLB=0x2E;
	USARTC0.BAUDCTRLA=0x98;
	
	//Enable RX dan TX
	USARTC0.CTRLB= USART_RXEN_bm | USART_TXEN_bm;
	
	//Enable RX Interrupt
	USARTC0.CTRLA = USART_RXCINTLVL_LO_gc;	
}

char myUART_Bufgetchar(void)
{
	static char data=0;
	
	//while (rx_counter0==0);
	data=rx_buffer0[rx_rd_index0++];
	
	if (rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
	cli();
	--rx_counter0;	//Atomic process
	sei();
	return data;
}